﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;

public class Location
{
    public Location()
    {

    }
    public static List<tbLocation> SelectLocationList(int FkParentID)
    {
        ICTBillingEntities billingEntities = new ICTBillingEntities();
        return billingEntities.tbLocations.Where(location => location.FkParentID == FkParentID).ToList();
    }
    public static tbLocation Select(int? PkLocationID)
    {
        ICTBillingEntities billingEntities = new ICTBillingEntities();
        return billingEntities.tbLocations.Where(location => location.PkLocationID == PkLocationID).ToList().FirstOrDefault();
    }
    [DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
    public static void Insert(string LocationName, int FkParentID, byte Type)
    {
        ICTBillingEntities billingEntities = new ICTBillingEntities();
        tbLocation location = new tbLocation(LocationName, FkParentID, Type);
        billingEntities.tbLocations.Add(location);
        billingEntities.SaveChanges();
    }
    [DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
    public static void Update(int PkLocationID, string LocationName, int FkParentID, byte Type)
    {
        ICTBillingEntities billingEntities = new ICTBillingEntities();        
        //TODO:
        tbLocation location = billingEntities.tbLocations.First(item => item.PkLocationID == PkLocationID);
        billingEntities.tbLocations.Remove(location);
        location = new tbLocation(PkLocationID, LocationName, FkParentID, Type);
        billingEntities.tbLocations.Add(location);
        billingEntities.SaveChanges();
    }
    [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
    public static void Delete(int PkLocationID)
    {
        ICTBillingEntities billingEntities = new ICTBillingEntities();
        tbLocation location = billingEntities.tbLocations.First(item => item.PkLocationID == PkLocationID);
        billingEntities.tbLocations.Remove(location);
        billingEntities.SaveChanges();
    }
    private static object ConvertNullParameter(object Value)
    {
        return (Value == null ? DBNull.Value : Value);
    }
}